package cn.stylefeng.guns.modular.goods.controller;

import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.goods.entity.LiuGoods;
import cn.stylefeng.guns.modular.goods.model.params.LiuGoodsParam;
import cn.stylefeng.guns.modular.goods.service.LiuGoodsService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.warpper.DeptTreeWrapper;
import cn.stylefeng.guns.sys.modular.system.warpper.DeptWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author lcl
 * @Date 2019-07-09 11:16:52
 */
@Controller
@RequestMapping("/liuGoods")
public class LiuGoodsController extends BaseController {


    private String PREFIX = "/liuGoods";

    @Autowired
    private LiuGoodsService liuGoodsService;



    /**
     * 跳转到主页面
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/liuGoods.html";
    }

    /**
     * 新增页面
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/liuGoods_add.html";
    }

    /**
     * 编辑页面
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/liuGoods_edit.html";
    }

    /**
     * 获取部门的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.liuGoodsService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 获取部门的tree列表，treeview格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */
    @RequestMapping(value = "/treeview")
    @ResponseBody
    public List<TreeviewNode> treeview() {
        List<TreeviewNode> treeviewNodes = this.liuGoodsService.treeviewNodes();
        //构建树
        DefaultTreeBuildFactory<TreeviewNode> factory = new DefaultTreeBuildFactory<>();
        factory.setRootParentId("0");
        List<TreeviewNode> results = factory.doTreeBuild(treeviewNodes);

        //把子节点为空的设为null
        DeptTreeWrapper.clearNull(results);

        return results;
    }








    /**
     * 新增接口
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(LiuGoodsParam liuGoodsParam) {
        this.liuGoodsService.add(liuGoodsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(LiuGoodsParam liuGoodsParam) {
        this.liuGoodsService.update(liuGoodsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("/delete")
    @Permission
    @ResponseBody
    public ResponseData delete( @RequestParam Long id) {
       if(ToolUtil.isEmpty(id)){
        throw   new ServiceException(BizExceptionEnum.REQUEST_NULL);
       }
      this.liuGoodsService.getByEmId(id);

       return  SUCCESS_TIP;

    }

    /**
     * 查看详情接口
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(LiuGoodsParam liuGoodsParam) {
        LiuGoods detail = this.liuGoodsService.getById(liuGoodsParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author lcl
     * @Date 2019-07-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(@RequestParam(value = "condition", required = false) String condition,
                              @RequestParam(value = "brand_id", required = false) Long brandId) {
        Page<Map<String, Object>> list = this.liuGoodsService.list(condition, brandId);
        Page<Map<String, Object>> wrap = new DeptWrapper(list).wrap();

        return LayuiPageFactory.createPageInfo(wrap);
    }


}


